﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsAuditDAL.cs
/// 作  者：SAM
/// 时  间：2006.11.05
/// 说  明：ClsAuditDAL类用于确认
/// </summary>
public class ClsAuditDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有的确认信息
    /// </summary>
    /// <returns>返回确认信息数据集</returns>
    public DataSet GetAudits(string strorderID)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetAudit", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@orderID", strorderID);//单据编号

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 新增确认记录
    /// </summary>
    /// <param name="strorderID">单据ID</param>
    /// <param name="strauditor">确认人</param>
    /// <param name="strauditStat">确认状态</param>
    /// <param name="strauditRemark">确认批语</param>
    /// <returns></returns>
    public Boolean AddAudit(string strorderID, string strauditor, bool strauditStat, string strauditRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddAudit", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@orderID", SqlDbType.NVarChar, 20);
        com.Parameters["@orderID"].Value = strorderID;//单据编号
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = strauditor;//确认人
        com.Parameters.Add("@auditStat", SqlDbType.Bit);
        com.Parameters["@auditStat"].Value = strauditStat;//确认状态
        com.Parameters.Add("@auditRemark", SqlDbType.NVarChar, 255);
        com.Parameters["@auditRemark"].Value = strauditRemark;//确认批语

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改此单在该单据类型中的确认状态
    /// </summary>UPDATE BY KIDY ON 20070124
    /// <param name="C_No">单据号</param>
    /// <param name="C_Type">单据类型</param>
    /// <param name="Auditor">终审人</param>
    /// <returns></returns>
    public string UpdateAutdiStat(string C_No, int C_Type, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdateAutdiStat", con);      
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@c_no", SqlDbType.NVarChar, 30);
        com.Parameters["@c_no"].Value = C_No;
        com.Parameters.Add("@c_type", SqlDbType.Int, 4);
        com.Parameters["@c_type"].Value = C_Type;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;

        try
        {
            con.Open();
            SqlDataReader rs = com.ExecuteReader();
            string strState = "";
            string strReIm_no = "";
            string strReLot_no = "";
            while (rs.Read())
            { 
                 strState = rs[0].ToString();//返回状态（N:未入库;Q:库存不足）
                 strReIm_no = rs[1].ToString();//返回的商品号
                 strReLot_no = rs[2].ToString();//返回的批号
            }
            
            if (strState == "N")
            {
                return "N" + "|" + strReIm_no + "|" + strReLot_no;
            }
            else if (strState == "Q")
            {
                return "Q" + "|" + strReIm_no + "|" + strReLot_no;
            }
            else
            {
                return "0";
            }
        }
        catch
        {
            return "";
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 取消确认时作废确认记录
    /// </summary>
    /// <param name="C_No">单据编号</param>
    /// <returns></returns>
    public Boolean DeleteAuditMsg(string C_No)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteAuditMsg", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@c_no", SqlDbType.NVarChar, 30);
        com.Parameters["@c_no"].Value = C_No;

        try
        {
            con.Open();

            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 获取当前登录用户在确认设置中对应的岗位
    /// </summary>
    /// <param name="strUserID">当前用户登录ID</param>
    /// <param name="OrderType">单据类型ID</param>
    /// <returns></returns>
    public string GetUserAuditStation(string strUserID, int OrderType)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_GetUserAuditLevel", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@strUserID", SqlDbType.NVarChar, 60);
        com.Parameters["@strUserID"].Value = strUserID;
        com.Parameters.Add("@order_type", SqlDbType.Int, 4);
        com.Parameters["@order_type"].Value = OrderType;

        try
        {
            con.Open();
            return com.ExecuteScalar().ToString();
        }
        catch
        {
            return "";
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 获取该单据还未确认的第一个岗位
    /// </summary>
    /// <param name="OrderID">单据编号</param>
    /// <param name="OrderType">单据类型</param>
    /// <returns></returns>
    public string GetOrderNoAuditFirstStation(string OrderID, int OrderType)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_GetOrderAuditNum", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@order_id", SqlDbType.NVarChar, 30);
        com.Parameters["@order_id"].Value = OrderID;
        com.Parameters.Add("@order_type", SqlDbType.Int, 4);
        com.Parameters["@order_type"].Value = OrderType;

        try
        {
            con.Open();
            return com.ExecuteScalar().ToString();
        }
        catch
        {
            return "";
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 获取该单据终审岗位
    /// </summary>
    /// <param name="OrderType">单据类型ID</param>
    /// <returns></returns>
    public string GetEndAuditStation(int OrderType)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_GetEndAuditStation", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@order_type", SqlDbType.Int, 4);
        com.Parameters["@order_type"].Value = OrderType;

        try
        {
            con.Open();
            return com.ExecuteScalar().ToString();
        }
        catch
        {
            return "";
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 验证价格调整权限
    /// </summary>
    /// <param name="UserID">当前登录用户ID</param>
    /// <returns></returns>
    public DataSet ValidatorPrice(string UserID)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_ValidatorPrice", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@u_log_id", SqlDbType.NVarChar, 60);
        dat.SelectCommand.Parameters["@u_log_id"].Value = UserID;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    //public int AdvSearch()
    //{

    //}
}
